<?php
/**
 * WooCommerce 样式覆盖和优化
 * WooCommerce Style Override and Optimization
 * 
 * @package WP Bootstrap Woo
 */

defined('ABSPATH') || exit;

/**
 * 禁用WooCommerce默认样式
 * Disable WooCommerce default styles
 */
function wbw_disable_woocommerce_styles() {
    // 检查是否启用了禁用WooCommerce样式的选项
    if (_xin('disable_woocommerce_styles', true)) {
        
        // 完全禁用WooCommerce样式表
        add_filter('woocommerce_enqueue_styles', '__return_empty_array');
        
        // 移除WooCommerce默认样式
        remove_action('wp_enqueue_scripts', 'wc_frontend_scripts');
        
        // 禁用特定的WooCommerce样式
        add_action('wp_enqueue_scripts', function() {
            wp_dequeue_style('woocommerce-general');
            wp_dequeue_style('woocommerce-layout');
            wp_dequeue_style('woocommerce-smallscreen');
            wp_dequeue_style('woocommerce_frontend_styles');
            wp_dequeue_style('woocommerce_fancybox_styles');
            wp_dequeue_style('woocommerce_chosen_styles');
            wp_dequeue_style('woocommerce_prettyPhoto_css');
            wp_dequeue_style('wc-block-style');
            wp_dequeue_style('wc-blocks-style');
        }, 100);
        
        // 移除WooCommerce的内联样式
        add_action('wp_enqueue_scripts', function() {
            wp_add_inline_style('theme-style1-css', '
                /* 覆盖WooCommerce默认样式 */
                .woocommerce .woocommerce-error,
                .woocommerce .woocommerce-info,
                .woocommerce .woocommerce-message {
                    border: none !important;
                    background: none !important;
                    color: inherit !important;
                }
                
                /* 确保产品网格使用我们的样式 */
                .woocommerce ul.products {
                    display: grid !important;
                    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)) !important;
                    gap: 20px !important;
                    margin: 0 !important;
                    padding: 0 !important;
                    list-style: none !important;
                }
                
                .woocommerce ul.products li.product {
                    margin: 0 !important;
                    padding: 0 !important;
                    border: none !important;
                    background: none !important;
                }
            ');
        }, 999);
    }
}
add_action('init', 'wbw_disable_woocommerce_styles');

/**
 * 确保主题样式优先级
 * Ensure theme styles priority
 */
function wbw_ensure_theme_styles_priority() {
    add_action('wp_enqueue_scripts', function() {
        // 确保主题样式在最后加载
        $template = get_current_homepage_template();
        $template_uri = get_template_directory_uri();
        
        // 重新排列样式加载顺序
        wp_enqueue_style('wbw-override-styles', $template_uri . '/inc/override-styles.css', array(), _THEME_VERSION, 'all');
        
        // 添加内联CSS来确保样式优先级
        $override_css = '
        /* 强制覆盖WooCommerce样式 */
        .woocommerce-page .content-area,
        .woocommerce .content-area {
            width: 100% !important;
            float: none !important;
            margin: 0 !important;
            padding: 0 !important;
        }
        
        /* 产品页面样式覆盖 */
        .woocommerce div.product,
        .woocommerce #content div.product,
        .woocommerce-page div.product,
        .woocommerce-page #content div.product {
            margin-bottom: 0 !important;
        }
        
        /* 购物车样式覆盖 */
        .woocommerce table.cart,
        .woocommerce-page table.cart {
            border: none !important;
            border-collapse: separate !important;
        }
        
        /* 结账页面样式覆盖 */
        .woocommerce form .form-row,
        .woocommerce-page form .form-row {
            padding: 3px !important;
            margin: 0 0 6px !important;
        }
        
        /* 按钮样式覆盖 */
        .woocommerce a.button,
        .woocommerce button.button,
        .woocommerce input.button,
        .woocommerce #respond input#submit {
            background: var(--primary-color, #007cba) !important;
            color: #fff !important;
            border: none !important;
            padding: 10px 20px !important;
            border-radius: 4px !important;
            text-decoration: none !important;
            display: inline-block !important;
            cursor: pointer !important;
            transition: all 0.3s ease !important;
        }
        
        .woocommerce a.button:hover,
        .woocommerce button.button:hover,
        .woocommerce input.button:hover {
            background: var(--primary-hover-color, #005a8b) !important;
            color: #fff !important;
        }
        
        /* 确保响应式设计 */
        @media (max-width: 768px) {
            .woocommerce ul.products {
                grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)) !important;
                gap: 15px !important;
            }
        }
        
        @media (max-width: 480px) {
            .woocommerce ul.products {
                grid-template-columns: 1fr 1fr !important;
                gap: 10px !important;
            }
        }
        ';
        
        wp_add_inline_style('wbw-override-styles', $override_css);
        
    }, 999);
}
add_action('init', 'wbw_ensure_theme_styles_priority');

/**
 * 创建WooCommerce样式覆盖CSS文件
 * Create WooCommerce style override CSS file
 */
function wbw_create_override_css_file() {
    $override_css_path = get_template_directory() . '/inc/override-styles.css';
    
    if (!file_exists($override_css_path)) {
        $css_content = '/*
* WooCommerce 样式覆盖文件
* WooCommerce Style Override File
* 自动生成 - Auto Generated
*/

/* 重置WooCommerce基础样式 */
.woocommerce * {
    box-sizing: border-box;
}

/* 产品列表样式 */
.woocommerce ul.products {
    list-style: none !important;
    margin: 0 !important;
    padding: 0 !important;
    display: grid !important;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)) !important;
    gap: 20px !important;
}

.woocommerce ul.products li.product {
    margin: 0 !important;
    padding: 0 !important;
    background: #fff !important;
    border: 1px solid #eee !important;
    border-radius: 8px !important;
    overflow: hidden !important;
    transition: transform 0.3s ease, box-shadow 0.3s ease !important;
}

.woocommerce ul.products li.product:hover {
    transform: translateY(-5px) !important;
    box-shadow: 0 10px 25px rgba(0,0,0,0.1) !important;
}

/* 产品图片样式 */
.woocommerce ul.products li.product .woocommerce-loop-product__link {
    display: block !important;
    text-decoration: none !important;
    color: inherit !important;
}

.woocommerce ul.products li.product img {
    width: 100% !important;
    height: auto !important;
    display: block !important;
    border: none !important;
}

/* 产品信息样式 */
.woocommerce ul.products li.product .woocommerce-loop-product__title {
    font-size: 16px !important;
    font-weight: 500 !important;
    margin: 10px !important;
    color: #333 !important;
    text-decoration: none !important;
}

/* 价格样式 */
.woocommerce ul.products li.product .price {
    margin: 0 10px 15px !important;
    font-size: 18px !important;
    font-weight: 600 !important;
    color: #e74c3c !important;
}

.woocommerce ul.products li.product .price del {
    color: #999 !important;
    text-decoration: line-through !important;
    margin-right: 8px !important;
}

/* 添加到购物车按钮 */
.woocommerce ul.products li.product .button {
    width: calc(100% - 20px) !important;
    margin: 0 10px 15px !important;
    background: #007cba !important;
    color: #fff !important;
    border: none !important;
    padding: 10px !important;
    border-radius: 4px !important;
    cursor: pointer !important;
    text-align: center !important;
    text-decoration: none !important;
    transition: background 0.3s ease !important;
}

.woocommerce ul.products li.product .button:hover {
    background: #005a8b !important;
    color: #fff !important;
}

/* 单个产品页面样式 */
.woocommerce div.product {
    display: grid !important;
    grid-template-columns: 1fr 1fr !important;
    gap: 40px !important;
    margin-bottom: 40px !important;
}

/* 购物车页面样式 */
.woocommerce table.cart {
    width: 100% !important;
    border-collapse: collapse !important;
    margin-bottom: 20px !important;
}

.woocommerce table.cart th,
.woocommerce table.cart td {
    padding: 15px !important;
    border-bottom: 1px solid #eee !important;
    vertical-align: middle !important;
}

/* 结账页面样式 */
.woocommerce .col2-set {
    display: grid !important;
    grid-template-columns: 1fr 1fr !important;
    gap: 40px !important;
}

/* 响应式设计 */
@media (max-width: 768px) {
    .woocommerce ul.products {
        grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)) !important;
        gap: 15px !important;
    }
    
    .woocommerce div.product {
        grid-template-columns: 1fr !important;
        gap: 20px !important;
    }
    
    .woocommerce .col2-set {
        grid-template-columns: 1fr !important;
        gap: 20px !important;
    }
}

@media (max-width: 480px) {
    .woocommerce ul.products {
        grid-template-columns: 1fr 1fr !important;
        gap: 10px !important;
    }
}

/* 隐藏WooCommerce默认的通知样式 */
.woocommerce-message,
.woocommerce-error,
.woocommerce-info {
    border: none !important;
    background: transparent !important;
    padding: 15px !important;
    margin: 20px 0 !important;
    border-radius: 4px !important;
}

.woocommerce-message {
    background: #d4edda !important;
    color: #155724 !important;
    border-left: 4px solid #28a745 !important;
}

.woocommerce-error {
    background: #f8d7da !important;
    color: #721c24 !important;
    border-left: 4px solid #dc3545 !important;
}

.woocommerce-info {
    background: #d1ecf1 !important;
    color: #0c5460 !important;
    border-left: 4px solid #17a2b8 !important;
}
';
        
        // 创建文件
        file_put_contents($override_css_path, $css_content);
    }
}

// 在主题激活时创建覆盖CSS文件
add_action('after_switch_theme', 'wbw_create_override_css_file');

/**
 * 移除WooCommerce的默认包装器
 * Remove WooCommerce default wrappers
 */
remove_action('woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10);
remove_action('woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10);

/**
 * 添加自定义的WooCommerce包装器
 * Add custom WooCommerce wrappers
 */
add_action('woocommerce_before_main_content', 'wbw_wrapper_start', 10);
add_action('woocommerce_after_main_content', 'wbw_wrapper_end', 10);

function wbw_wrapper_start() {
    echo '<div id="woocommerce-content" class="woocommerce-content"><div class="container"><div class="row"><div class="col-12">';
}

function wbw_wrapper_end() {
    echo '</div></div></div></div>';
}

/**
 * 移除WooCommerce面包屑导航（如果需要的话）
 * Remove WooCommerce breadcrumbs if needed
 */
if (_xin('disable_wc_breadcrumbs', false)) {
    remove_action('woocommerce_before_main_content', 'woocommerce_breadcrumb', 20);
}

/**
 * 自定义WooCommerce产品循环
 * Customize WooCommerce product loop
 */
function wbw_custom_product_loop() {
    // 移除默认的产品标题和价格位置
    remove_action('woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10);
    remove_action('woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10);
    
    // 添加自定义的产品信息结构
    add_action('woocommerce_after_shop_loop_item', 'wbw_custom_product_info', 5);
}
add_action('init', 'wbw_custom_product_loop');

function wbw_custom_product_info() {
    global $product;
    echo '<div class="product-info">';
    echo '<h6 class="product-name"><a href="' . get_permalink() . '">' . get_the_title() . '</a></h6>';
    echo '<div class="product-price">' . $product->get_price_html() . '</div>';
    echo '</div>';
}

/**
 * 优化WooCommerce查询性能
 * Optimize WooCommerce query performance
 */
function wbw_optimize_woocommerce_queries($q) {
    if (!is_admin() && $q->is_main_query()) {
        if (is_shop() || is_product_category() || is_product_tag()) {
            // 设置合理的产品显示数量
            $q->set('posts_per_page', 12);
        }
    }
}
add_action('pre_get_posts', 'wbw_optimize_woocommerce_queries');

// 确保在主题激活时立即创建覆盖CSS文件
wbw_create_override_css_file();